package adapter;

import android.app.Activity;
import android.bluetooth.BluetoothDevice;
import android.bluetooth.BluetoothSocket;
import android.content.Context;
import android.os.Bundle;

import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.View;
import android.widget.Toast;

import com.chad.library.adapter.base.BaseQuickAdapter;
import com.sdwfqin.cbt.CbtManager;
import com.sdwfqin.cbt.callback.ConnectDeviceCallback;
import com.sdwfqin.cbt.callback.ScanCallback;

import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;
import de.greenrobot.event.EventBus;
import xm.chengdu.com.beijing_211.MyEvent;
import xm.chengdu.com.beijing_211.R;

/**
 * 描述：扫描设备
 *
 * @author zhangqin
 * @date 2018/5/30
 */
public class ScanListActivity extends Activity {

    @BindView(R.id.rv)
    RecyclerView mRv;



    private Context mContext;
    private ScanListAdapter mScanListAdapter;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_scan_list);
        ButterKnife.bind(this);
        mContext = this;
//        CbtManager.getInstance().stop();
        initList();
        initData();
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        CbtManager.getInstance().stop();

    }

    /**
     * 初始化列表
     */
    private void initList() {
        mRv.setLayoutManager(new LinearLayoutManager(mContext));
//        mRv.addItemDecoration(new DividerItemDecoration(mContext, DividerItemDecoration.VERTICAL));
        mScanListAdapter = new ScanListAdapter(null);
        mRv.setAdapter(mScanListAdapter);
        mScanListAdapter.setOnItemClickListener(new BaseQuickAdapter.OnItemClickListener() {
                                                    @Override
                                                    public void onItemClick(BaseQuickAdapter adapter, View view, int position) {


                                                        BluetoothDevice item = mScanListAdapter.getItem(position);
                                                        CbtManager
                                                                .getInstance()
                                                                .connectDevice(item, new ConnectDeviceCallback() {
                                                                    @Override
                                                                    public void connectSuccess(BluetoothSocket socket, BluetoothDevice device) {
                                                                        MyEvent myEvent = new MyEvent();
                                                                        myEvent.setType("lianjie");
                                                                        myEvent.setSocket(socket);
                                                                        EventBus.getDefault().post(myEvent);

                                                                        finish();
                                                                    }

                                                                    @Override
                                                                    public void connectError(Throwable throwable) {
//
                                                                        Toast.makeText(mContext, throwable.getMessage(), Toast.LENGTH_SHORT).show();
                                                                    }
                                                                });
                                                    }
                                                }
        );


    }

    /**
     * 加载数据
     */
    private void initData() {
        CbtManager
                .getInstance()
                .scan(new ScanCallback() {
                    @Override
                    public void onScanStart(boolean isOn) {

                    }

                    @Override
                    public void onScanStop(List<BluetoothDevice> devices) {
                        mScanListAdapter.setNewData(devices);
                    }

                    @Override
                    public void onFindDevice(BluetoothDevice device) {

                        mScanListAdapter.addData(device);
                    }
                });
    }

    @OnClick({R.id.imageView1 })
    public void onViewClicked(View view) {
        switch (view.getId()) {
            case R.id.imageView1:
                finish();
                break;

        }
    }
}
